﻿namespace WLFramework.Web.Binder.ValueProvider
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.SessionState;

    public class SessionValueProvider : IValueProvider
    {
        private HttpSessionState values;

        public SessionValueProvider()
            : this(HttpContext.Current.Session)
        {
        }

        public SessionValueProvider(HttpSessionState session)
        {
            this.values = session;
        }
        
        public ValueProviderResult GetValue(string key)
        {
            if (this.values == null) return new ValueProviderResult(null);
            var rawValue = this.values[key];
            return new ValueProviderResult(rawValue);
        }
    }

    #if Net40
    public class SessionBaseValueProvider : IValueProvider
    {
        private HttpSessionStateBase values;

        public SessionBaseValueProvider(HttpSessionStateBase session)
        {
            this.values = session;
        }

        public ValueProviderResult GetValue(string key)
        {
            var rawValue = this.values[key];
            if (rawValue == null)
            {
                return null;
            }

            return new ValueProviderResult(rawValue);
        }
    }

#endif
}
